package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProductDevelopListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProductSalesOriginal;
import com.isunland.managesystem.entity.ProductSalesSearchContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by lenovo on 2016/6/15.
 * 产品开发列表
 */
public class ProductDevelopListFragment extends BaseListFragment {
  private static final int PRODUCT_SEARCH = 0;
  private static final int PRODUCT_COUNT = 1;
  private static final int PRODUCT_DETAIL = 2;
  private static final int PRODUCT_ADD = 3;
  private CurrentUser mCurrentUser;
  private ProductDevelopListAdapter mAdapter;
  private ArrayList<ProductSalesOriginal.ProductSalesContent> mList;

  private String startDate =
      MyDateUtil.dateToString(MyDateUtil.getBeforeMonth(3), MyDateUtil.TYPE_MONTH);
  private String endDate = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_MONTH);
  private String salePersonName = "";
  private String salePersonId = "";
  private String customerName = "";
  private String customerId = "";
  private String productName = "";
  private String productId = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar;
    if (activity != null) {
      actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(activity) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle(R.string.productDevelopList);
      }
    }
    mActivity = (BaseVolleyActivity) getActivity();

    mCurrentUser = CurrentUser.newInstance(getActivity());
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_product_sales, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

            /*添加*/
      case R.id.menu_item_add:
        Intent i = new Intent(getActivity(), ProductDevelopDetailActivity.class);
        startActivityForResult(i, PRODUCT_ADD);
        break;
            /*查询*/
      case R.id.menu_item_query:
        ProductSalesSearchContent content =
            new ProductSalesSearchContent(startDate, endDate, salePersonName, salePersonId,
                customerName, customerId, productName, productId);
        Intent query = new Intent(getActivity(), ProductSaleSearchActivity.class);
        query.putExtra(ProductSaleSearchFragment.EXTRA_SEARCH, content);
        startActivityForResult(query, PRODUCT_SEARCH);

        break;
            /*统计*/
      case R.id.menu_item_contract:
        //                ProductSalesSearchContent content1=new ProductSalesSearchContent(startDate,endDate,salePersonName,
        //                        salePersonId,customerName, customerId,productName,productId);
        //                Intent count=new Intent(getActivity(),ProductSaleCountActivity.class);
        //                count.putExtra(ProductSaleCountFragment.EXTRA_VALUE,content1);
        //                startActivityForResult(count,PRODUCT_SEARCH);
        ProductSalesSearchContent content1 =
            new ProductSalesSearchContent(startDate, endDate, salePersonName, salePersonId,
                customerName, customerId, productName, productId);
        Intent count = new Intent(getActivity(), ProductDevelopCountActivity.class);
        count.putExtra(ProductDevelopCountFragment.EXTRA_VALUE, content1);
        startActivityForResult(count, PRODUCT_SEARCH);

        break;
      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.ULR_PRODUCTDEVELOP_LIST;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<>();
    params.put("beginInMonth", startDate);
    params.put("endInMonth", endDate);
    params.put("customer", customerName);
    params.put("salesManName", salePersonName);//业务员
    params.put("product", productName);
    params.put("memberCode", mCurrentUser.getMemberCode());
    return params;
  }

  @Override public void resolveResponse(String json) {
    ProductSalesOriginal original = new Gson().fromJson(json, ProductSalesOriginal.class);
    ArrayList<ProductSalesOriginal.ProductSalesContent> rows =
        (ArrayList<ProductSalesOriginal.ProductSalesContent>) original.getRows();
    if (mList == null) {
      mList = new ArrayList<>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<ProductSalesOriginal.ProductSalesContent> mList) {
    if (mAdapter == null) {
      mAdapter = new ProductDevelopListAdapter(getActivity(), mList);
    }
    setListAdapter(mAdapter);
    ((ProductDevelopListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ProductSalesOriginal.ProductSalesContent content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), ProductDevelopDetailActivity.class);
    i.putExtra(ProductDevelopDetailFragment.EXTRA_CONTENT, content);
    startActivityForResult(i, PRODUCT_DETAIL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    //查询
    if (requestCode == PRODUCT_SEARCH) {
      ProductSalesSearchContent content = (ProductSalesSearchContent) data.getSerializableExtra(
          ProductSaleSearchFragment.EXTRA_SEARCH);
      startDate = content.getStartDate();
      endDate = content.getEndDate();
      salePersonName = content.getSalePersonName();
      salePersonId = content.getSalePersonId();
      customerName = content.getCustomerName();
      customerId = content.getCustomerId();
      productName = content.getProductName();
      productId = content.getProductId();

      volleyPost();
    }
    //添加
    if (requestCode == PRODUCT_ADD) {
      volleyPost();
    }
    //统计
    //noinspection StatementWithEmptyBody
    if (requestCode == PRODUCT_COUNT) {

    }
    //详情
    if (requestCode == PRODUCT_DETAIL) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}

